package com.sbwebsocket.core.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * @Auther: GSJ
 * @Date: 2022/2/16
 * @Description:
 * @version: 1.0
 */
@ConfigurationProperties(prefix = "ws")
@Data
public class WsAppConfig {

    @Autowired
    private Environment environment;
    //主节点续租时间
    private long masterLeaseTime = 5000;
    //异常消息最大留存时间
    private long unusualMessageLingerMaxTime = 1 * 60 * 1000;
    //消息队列中件间实现(1:redis(默认)
    private String messageQueueChoose = "redis";

    @Bean
    ScheduledExecutorService scheduledThreadPoolExecutor() {
        ScheduledExecutorService scheduledExecutorService =
                Executors.newSingleThreadScheduledExecutor(r -> {
                    Thread thread = new Thread(r);
                    thread.setName("scheduledTaskWSMasterChoose");
                    return thread;
                });
        return scheduledExecutorService;
    }

    public String getHostKey() {
        String ip = InetAddress.getLoopbackAddress().getHostAddress();
        return ip + ":" + environment.getProperty("server.port");
    }
}
